using System;
using System.Collections.Generic;
using DL.Tools;

namespace DL.CfgData
{
    public class ActivityInfo
    {
        public int id { get; set; }
        public string name { get; set; }
        public DateTime st { get; set; }
        public DateTime et { get; set; }
        
        public ActivityInfo(int id, string name, string st, string et)
        {
            this.id = id;
            this.name = name;
            this.st = DateTime.Parse(st);
            this.et = DateTime.Parse(et);
        }

        //判断是否过期
        public bool IsExpired(DateTime nowTime)
        {
            Debugger.EO_Log($"st:{st},nt:{nowTime},et:{et}");
            return (st > nowTime || et < nowTime);
        }

        public void Print()
        {
            Debugger.EO_Log($"id:{id},name:{name},st:{st},et:{et}");
        }
    }

    public class ActivityCfgData : TextCfgData
    {
        public override string ConfigFileName => "Activity";
        private List<ActivityInfo> m_ActivityInfos;
         
        protected override void Load(string cfgDataContent)
        {
            Reset();
            Debugger.EO_Log(cfgDataContent);
            if (string.IsNullOrEmpty(cfgDataContent))
            {
                Debugger.EO_Log("未读取到对应Json信息" + ConfigFileName);
                return;
            }
            var list = MiniJson.Json.Deserialize(cfgDataContent) as List<object>;
            
            foreach (Dictionary<string,object> datas in list)
            {
                datas.TryGetValue("id", out var id);
                datas.TryGetValue("name", out var name);
                datas.TryGetValue("st", out var st);
                datas.TryGetValue("et", out var et);
                var item = new ActivityInfo(Convert.ToInt32(id), name.ToString(), st.ToString(), et.ToString());
                m_ActivityInfos.Add(item);
            }
        }

        private void Reset()
        {
            m_ActivityInfos ??= new List<ActivityInfo>();
            m_ActivityInfos.Clear();
        }

        public List<ActivityInfo> GetAllExpiredActivity()
        {
            var nowTime = DateTime.UtcNow;
            return m_ActivityInfos.FindAll(x => x.IsExpired(nowTime));
        }

        public List<ActivityInfo> ActivityInfos => m_ActivityInfos;
    }
}
